﻿using UnityEngine;
using System.Collections;
using System;

public class NetHandle : MonoBehaviour {

    #region 单例

    private static NetHandle s_instance = null;

    public static NetHandle Instance {
        get {
            s_instance = UnityEngine.Object.FindObjectOfType(typeof(NetHandle)) as NetHandle;
            if (s_instance == null) {
                GameObject obj2 = new GameObject("NetHandle");
                s_instance = obj2.AddComponent(typeof(NetHandle)) as NetHandle;
                if (s_instance != null) {
                    s_instance.Init( );
                }
            }
            return s_instance;
        }
    }

    #endregion 单例   

    void OnDestroy ( ) {
        m_isQuit = true;
    }

    private void Init ( ) {
        m_isQuit = false;
        InitSocket( );

        DontDestroyOnLoad(this);
    }

    private bool m_isQuit = false;

    public void InitSocket ( ) {        
        NetWriter.SetUrl(Const.URL);
        StartCoroutine(HeartbeatReq());
    }

    #region 请求
    
    // 心跳
    public IEnumerator HeartbeatReq ( ) {  
        while (!m_isQuit) {
            HelloAction(1);
            yield return new WaitForSeconds(2);
        }
    }

    public void HelloAction (int ops) {
        ActionParam ap = new ActionParam( );
        ap["ops"] = ops;        
        Net.Instance.Send((int)ActionType.Hello, null, ap);
    }

    public void LoginReq (Action<ActionResult> finish) {
        Net.Instance.Send((int)ActionType.Login, finish, null);
    }

    public void RegisterReq (Action<ActionResult> finish) {
        Net.Instance.Send((int)ActionType.Regis, finish, null);
    }

    // 创建角色请求
    public void CreateRoteReq (Action<ActionResult> finish,
                                                    string userName = "unity3dgzc",
                                                    byte sex = 1,
                                                    string headID = "head.gif",
                                                    string retailID = "0000",
                                                    string pid = "Z83742",
                                                    short mobileType = 1,
                                                    short ScreenX = 100,
                                                    short ScreenY = 100,
                                                    short ClientAppVersion = 1,
                                                    int GameID = 7,
                                                    int ServerID = 1) {
        ActionParam ap = new ActionParam( );
        ap["UserName"] = userName;
        ap["Sex"] = sex;
        ap["HeadID"] = headID;
        ap["RetailID"] = retailID;
        ap["Pid"] = pid;
        ap["MobileType"] = mobileType;

        ap["ScreenX"] = ScreenX;
        ap["ScreenY"] = ScreenY;
        ap["ClientAppVersion"] = ClientAppVersion;
        ap["GameID"] = GameID;
        ap["ServerID"] = ServerID;
        // 请求
        Net.Instance.Send((int)ActionType.CreateRote, finish, ap);
    }

    public void MainUiReq (Action<ActionResult> finish) {
        Net.Instance.Send((int)ActionType.MainUI, finish, null);
    }

    public void EnterRoomReq (Action<ActionResult> finish, int roomId = 1001, int op = 1) {
        ActionParam ap = new ActionParam( );
        ap["RoomId"] = roomId;
        ap["Op"] = op;
        // 请求
        Net.Instance.Send((int)ActionType.EnterRoom, finish, ap);
    }

    public void EnterDeskReq (Action<ActionResult> finish) {
        Net.Instance.Send((int)ActionType.EnterDesk, finish, null);
    }

    // 发牌
    public void DealReq (Action<ActionResult> finish) {
        Net.Instance.Send((int)ActionType.Deal, finish, null);
    }

    // 叫地主，1：叫，2：不叫
    public void CallLandlordReq (int op) {
        ActionParam ap = new ActionParam( );
        ap["op"] = op;
        Net.Instance.Send((int)ActionType.CallLandlord, null, ap);
    }

    public void CallLandlordResultReq (byte isCall,Action<ActionResult> finish) {
        ActionParam ap = new ActionParam( );
        ap["IsCall"] = isCall;
        Net.Instance.Send((int)ActionType.CallLandlordResult, finish, ap);
    }

    // 明牌接口 op 1：明牌，2：不明牌 
    public void MingPaiReq (int op, Action<ActionResult> finish) {
        ActionParam ap = new ActionParam( );
        ap["Op"] = op;
        Net.Instance.Send((int)ActionType.MingPai, finish, ap);
    }

    /// <summary>
    /// 出牌接口
    /// </summary>
    /// <param name="cards">卡牌，多张以，号分隔；空串则不出牌,牌组以小到大的顺序 出牌后通知</param>  
    public void ChuPaiReq (string  cards) {
        if (string.IsNullOrEmpty(cards)) return;
        ActionParam ap = new ActionParam( );
        ap["Cards"] = cards;
        Net.Instance.Send((int)ActionType.ChuPai, null, ap);
    }
    // 出牌通知接口
    public void ChuPaiInformReq (Action<ActionResult> finish) {
        Net.Instance.Send((int)ActionType.ChuPaiInform, finish, null);
    }

    #endregion 请求

}
